<?php

namespace plugin\ycMassage\app\api\controller;

use plugin\ycMassage\app\BaseController;
use plugin\ycMassage\app\model\MassageFeedback;
use plugin\ycMassage\app\model\MassageUser;
use plugin\ycMassage\utils\enum\FeedbackType;
use plugin\ycMassage\utils\enum\NoticeType;
use plugin\ycMassage\utils\notice\Notice;
use support\Request;

class FeedbackController extends BaseController
{
    protected $noNeedLogin = ["create"];
    public function create(Request $request)
    {
        $saas_appid = $request->appid;
        $uid = $request->uid;
        if ($request->saas_appid) {
            $saas_appid = $request->saas_appid;
        }
        $D = $request->post();
        if (empty($D['content'])) {
            return $this->fail('请填写反馈内容');
        }
        $D['saas_appid'] = $saas_appid;
        $D['uid'] = $uid;
        if (empty($D['feedback_type'])) {
            $D['feedback_type'] = FeedbackType::OTHER['value'];
        }
        $MassageFeedback = new MassageFeedback();
        if ($MassageFeedback->save($D)) {
            $notice = new Notice();
            $notice->saas_appid = $saas_appid;
            $notice->role = ['ADMIN'];
            $notice->type = NoticeType::FEEDBACK['value'];
            if ($uid) {
                $MassageUser = MassageUser::where(['id' => $uid])->find();
                $name = base64_decode($MassageUser->nickname) . '(' . $uid . ')';
            } else {
                $name = '游客';
            }
            $notice->setData([
                'service_name' => FeedbackType::getText($D['feedback_type'])['text'] . '反馈',
                'name' => $name,
                'time' => date('Y-m-d H:i:s'),
            ]);
            $notice->send();
            return $this->success('提交成功');
        }
        return $this->fail('提交失败');
    }
}
